package com.pug.controller.index;

import com.alibaba.fastjson.JSONObject;
import com.pug.annotation.EnableNoToken;
import com.pug.controller.BaseController;
import com.pug.pojo.Advice;
import com.pug.pojo.Banner;
import com.pug.pojo.Category;
import com.pug.service.advice.AdviceServiceImpl;
import com.pug.service.advice.AdviceServiceImplPool;
import com.pug.service.banner.BannerServiceImpl;
import com.pug.service.banner.BannerServiceImplPool;
import com.pug.service.category.CategoryServiceImpl;
import com.pug.service.category.CategoryServiceImplPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * Author: lqm
 * create: 2022/4/29 20:31
 * Lover: lqx
 *
 * @see
 */
@RestController
@Slf4j
public class IndexController extends BaseController {
    @Resource
    ThreadPoolTaskExecutor myThreadPoolExecutor;
    @Resource
    AdviceServiceImpl adviceService;
    @Resource
    BannerServiceImpl bannerService;
    @Resource
    CategoryServiceImpl categoryService;
    @Resource
    AdviceServiceImplPool adviceServiceImplPool;
    @Resource
    BannerServiceImplPool bannerServiceImplPool;
    @Resource
    CategoryServiceImplPool categoryServiceImplPool;
    //查询首页轮播图，通知，分类，通过jsonObject
    @PostMapping("/index/load")
    @EnableNoToken(true)
    JSONObject getIndexData() throws ExecutionException, InterruptedException {
        log.info("查询轮播图开始");
        Future<List<Advice>> submit = myThreadPoolExecutor.submit(adviceServiceImplPool);
        Future<List<Banner>> submit1 = myThreadPoolExecutor.submit(bannerServiceImplPool);
        Future<List<Category>> submit2 = myThreadPoolExecutor.submit(categoryServiceImplPool);
        /**
         * 非线程池
         */
//        List<Advice> indexAdvice = adviceService.findIndexAdvice(1, 6);  //查询通知
//        List<Banner> indexBanner = bannerService.findIndexBaner(1, 6);   //查询轮播图
//        List<Category> categories = categoryService.findCategories();    //查询商品分类
        /**
         * 使用线程池技术，将查询通知，查询轮播图，查询商品分类交给三个线程执行。
         */
        categoryServiceImplPool.findCategories();
        submit2.get();
        List<Advice> indexAdvice=null;
        List<Banner> indexBanner=null;
        List<Category> categories=null;
        indexAdvice=submit.get();
        indexBanner=submit1.get();
        categories=submit2.get();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("bannerList",indexBanner);
        jsonObject.put("categoryList",categories);
        jsonObject.put("adviceList",indexAdvice);
        log.info("查询轮播图结束");
        return jsonObject;
    }
}
